home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 8 / The Arsenal Files Collection #8 (Arsenal Computer) (1996).ISO / prg_casm / snip9611.zip / MKDIRS.C < prev    next >
C/C++ Source or Header  |  1996-11-24  |  2KB  |  70 lines

  1. /* +++Date last modified: 02-Sep-1996 */
  2.  
  3. /*
  4. **  MKDIRS.C - Function to build multi-level directories in a single call
  5. **
  6. **  Original Copyright 1993-95 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. **
  13. **  Also uses PUSHDIR.C from SNIPPETS.
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <io.h>
  20. #include "dosfiles.h"
  21. #include "unistd.h"
  22.  
  23. int mkdirs(char *pathname)
  24. {
  25.       int retval;
  26.       char path[FILENAME_MAX];
  27.  
  28.       strcpy (path, pathname);            /* isdir() may expand this    */
  29.  
  30.       while (strlen(path) && '\\' == LAST_CHAR(path))
  31.             LAST_CHAR(path) = NUL;
  32.  
  33.       while (0 != (retval = mkdir(path)))
  34.       {
  35.             char subpath[FILENAME_MAX] = "", *delim;
  36.  
  37.             if (EACCES == errno)
  38.             {
  39.                   if (isdir(path))
  40.                         return 0;
  41.                   else  return retval;
  42.             }
  43.             if (NULL == (delim = strrchr(path, '\\')))
  44.                   return retval;
  45.             strncat(subpath, path, delim - path);     /* Appends NUL    */
  46.             if (Success_ != mkdirs(subpath))
  47.                   break;
  48.       }
  49.       return retval;
  50. }
  51.  
  52. #ifdef TEST
  53.  
  54. main(int argc, char *argv[])
  55. {
  56.       if (2 > argc)
  57.       {
  58.             puts("Usage: MKDIRS pathname [...pathname]");
  59.             return -1;
  60.       }
  61.       while (--argc)
  62.       {
  63.             ++argv;
  64.             printf("mkdirs(%s) returned %d\n", *argv, mkdirs(*argv));
  65.       }
  66.       return 0;
  67. }
  68.  
  69. #endif /* TEST */
  70.